package com.zhufei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import   javax.servlet.jsp.PageContext;
import com.zhufei.dao.NewsDao;
import com.zhufei.dao.Paging;

public class newsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public newsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//接受操作新闻的动作
		String action = request.getParameter("action"); 
		
		if(action == null){
			
		}
		
		if(action.equals("add")){
			this.add(request,response);
		}
		if(action.equals("manage")){
			this.manage(request,response);
		}
		if(action.equals("delete")){
			this.delete(request,response);
		}
		if(action.equals("edit")){
			this.edit(request,response);
		}
		if(action.equals("showList")){
			this.showList(request,response);
		}
		
	}



	private void showList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		NewsDao news = new NewsDao();
		news.getConnection();
		int newsCount = news.getCountOfNews();
		ArrayList newsList = news.getNewses(0, newsCount);
		HttpSession session = request.getSession();
		session.setAttribute("newsList", newsList);
		Paging paging = new Paging();
		//取得page值
		String strPage = paging.strPage(request, "page");
		int current_Page = paging.current_Pages(strPage);
		//设置一页显示多少行数据
		int page_record = paging.setPage_record(10);
		//取得总页数
		int total_page = paging.getTotal_Pages(newsCount);
		//保存当前页数
		session.setAttribute("current_page", current_Page);
		//保存总页数
		session.setAttribute("total_page", total_page);
		session.setAttribute("page_record", page_record);
		response.sendRedirect("newsList.jsp");		
		
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String newsID = request.getParameter("id");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		NewsDao news = new NewsDao();
		news.getConnection();
		boolean isSucc = news.updateNews(newsID, title, content);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '编辑新闻成功！ ');window.location.href= 'newsServlet?action=manage '; </script> "); 
			out.flush(); 
			out.close(); 
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '编辑新闻失败！ ');window.location.href= 'newsServlet?action=manage '; </script> "); 
			out.flush(); 
			out.close(); 
			
		}
		

		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String newsId = request.getParameter("id");
		NewsDao news = new NewsDao();
		news.getConnection();
		boolean isSucc = news.deleteNews(newsId);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '删除成功！ ');window.location.href= 'newsServlet?action=manage '; </script> "); 
			out.flush(); 
			out.close(); 
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '删除失败！ ');window.location.href= 'newsServlet?action=manage '; </script> "); 
			out.flush(); 
			out.close(); 
		}
	}

	private void manage(HttpServletRequest request, HttpServletResponse response) throws IOException {

		NewsDao news = new NewsDao();
		news.getConnection();
		int newsCount = news.getCountOfNews();
		ArrayList newsList = news.getNewses(0, newsCount);
		HttpSession session = request.getSession();
		session.setAttribute("newsList", newsList);
		Paging paging = new Paging();
		//取得page值
		String strPage = paging.strPage(request, "page");
		int current_Page = paging.current_Pages(strPage);
		//设置一页显示多少行数据
		int page_record = paging.setPage_record(10);
		//取得总页数
		int total_page = paging.getTotal_Pages(newsCount);
		//保存当前页数
		session.setAttribute("current_page", current_Page);
		//保存总页数
		session.setAttribute("total_page", total_page);
		session.setAttribute("page_record", page_record);
		response.sendRedirect("admin/manageNews.jsp");
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String title = request.getParameter("title");
		String content = request.getParameter("content");

		NewsDao news = new NewsDao();
		news.getConnection();
		boolean isSucc = news.insertNews(title, content);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '添加新闻成功！ ');window.location.href= 'newsServlet?action=manage '; </script> "); 
			out.flush(); 
			out.close(); 

		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '添加新闻失败！ ');window.location.href= 'newsServlet?action=manage '; </script> "); 
			out.flush(); 
			out.close(); 			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
